package com.bawei.test.loadsir

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import androidx.databinding.DataBindingUtil
import com.bawei.test.R
import com.bawei.test.databinding.ActivityLoadSirBinding
import com.google.android.material.internal.ToolbarUtils
import com.kingja.loadsir.callback.Callback
import com.kingja.loadsir.callback.SuccessCallback
import com.kingja.loadsir.core.Convertor
import com.kingja.loadsir.core.LoadService
import com.kingja.loadsir.core.LoadSir

class LoadSirActivity : AppCompatActivity() {
    private lateinit var activityLoadSirBinding: ActivityLoadSirBinding
    private lateinit var loadService:LoadService<Any>
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        activityLoadSirBinding = DataBindingUtil.setContentView(this,R.layout.activity_load_sir)


        loadService = LoadSir.getDefault().register(this, Callback.OnReloadListener {
            Toast.makeText(this,"在此请求",Toast.LENGTH_SHORT).show()
        }, Convertor<ApiResponse> {
            var resultCode: Class<out Callback?> = SuccessCallback::class.java
            if(it.code == 200){
                if(it.data == null){
                    resultCode = EmptyCallback::class.java
                }else{
                    resultCode = SuccessCallback::class.java
                }
            }else{
                resultCode = ErrorCallback::class.java
            }
            resultCode
        })
        Thread{
            Thread.sleep(2000)
            loadService.showWithConvertor( ApiResponse(200,null))
        }.start()

//        //转2秒:网络请求2秒
//        Thread{
//            Thread.sleep(2000)
//            loadService.showCallback(EmptyCallback::class.java)
//            Thread.sleep(2000)
//            loadService.showCallback(ErrorCallback::class.java)
//            Thread.sleep(2000)
//            loadService.showCallback(TimeoutCallback::class.java)
//        }.start()


    }

   data class ApiResponse(val code:Int,val data:String?)
}